package com.buka.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.goods.mapper.BrandMapper;
import com.buka.goods.service.BrandService;
import com.buka.server.pojo.Brand;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itbuka.feign.MinioFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private MinioFeign minioFeign;
    @Override
    /**
     * 查询全部列表数据
     */
    public List<Brand> findAll() {
        Example example = new Example(Brand.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        List<Brand> list = brandMapper.selectByExample(example);
        return list;

    }

    @Override
    /**
     * 根据ID查询实体数据
     */
    public List<Brand> findById(Long id) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        criteria.andEqualTo("id", id);
        List<Brand> list = brandMapper.selectByExample(example);
        return list;
    }

    @Override
    /**
     * 增加品牌
     */
    public int addBrand(Brand brand) {
        long id = IdWorker.getId();
        brand.setId(id);
        int flag = brandMapper.insertSelective(brand);
        return flag;
    }

    @Override
    /**
     * 修改品牌
     */
    public int updateBrand(Brand brand) {

        int flag = brandMapper.updateByPrimaryKeySelective(brand);
        return flag;
    }

    /**
     * 真删
     */
    @Override
    public int deleteTrueBrand(Long id) {
        int flag = brandMapper.deleteByPrimaryKey(id);
        return flag;
    }


    /**
     * 假删
     *
     * @param id
     * @return
     */
    @Override
    public int deleteFalseBrand(Long id) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Brand brand = new Brand();
        brand.setIsDelete(1);

        return brandMapper.updateByExampleSelective(brand, example);
    }

    @Override
    public List<Brand> FuzzyQuery(String brandName) {

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        criteria.andLike("brandName", "%" + brandName + "%");
        List<Brand> list = brandMapper.selectByExample(example);
        return  list;
    }

    @Override
    public int OpenOrStopBrand(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);

        brand.setStatus(brand.getStatus() == 0 ? 1 : 0); // 修改状态值
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        brandMapper.updateByExampleSelective(brand, example); // 执行更新操作
        int flag =brand.getStatus();

        return flag;
    }

    @Override
    public Page<Brand> findPage(Integer page, Integer size) {

        PageHelper.startPage(page,size);

        return (Page<Brand>) brandMapper.selectAll();
    }

    @Override
    public Page<Brand> findPage(String brandName, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        return (Page<Brand>)this.FuzzyQuery(brandName);
    }

    @Override
    public Integer addAndFile(String brandName, MultipartFile file) {
        Brand brand = new Brand();
        String url = minioFeign.uploadF(file).getData();
        if (url.equals("1") || url.equals("2")) {
            return -1;
        }
        brand.setId(IdWorker.getId());
        brand.setBrandLogo(url);
        brand.setBrandName(brandName);
        int i = brandMapper.insertSelective(brand);
        return i;
    }


}
